package com.skyvin.nreceipts.admin.controller;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.skyvin.nreceipts.admin.mongorepository.ItemCategoryTemplateRepository;
import com.skyvin.nreceipts.admin.service.ReceiptItemCategoryTemplateService;
import com.skyvin.nreceipts.common.document.ReceiptItemCategoryTemplate;
import com.skyvin.nreceipts.common.dto.ReceiptItemCategoryDTO;
import com.skyvin.nreceipts.common.mapper.ReceiptItemCategoryMapper;
import com.skyvin.nreceipts.common.value.JQGridResponse;
import com.skyvin.nreceipts.common.value.MessageValue;

@Controller
@RequestMapping(value="itemCategoryTemplate")
public class ItemCategoryTemplateController {
	private static Logger logger = Logger.getLogger(ItemCategoryTemplateController.class);
	
	@Autowired
	private ItemCategoryTemplateRepository itemCategoryTemplateRepository; 
	
	@Autowired
	private ReceiptItemCategoryTemplateService receiptItemCategoryTemplateService; 

	
	@RequestMapping(value="/loadItemCategoryPage", method=RequestMethod.GET)
	@PreAuthorize("hasAuthority('ADMIN')")
	public String loadReceiptItemCategory() {
		return "admin/receiptItemCategoryTemplateEdit";
	}
	
	@RequestMapping(value="/viewItemCategories", method=RequestMethod.GET)
	@ResponseBody
	public JQGridResponse viewReceiptItemCategories(@RequestParam("_search") Boolean search,
    		@RequestParam(value="filters", required=false) String filters,
    		@RequestParam(value="page", required=false) Integer page,
    		@RequestParam(value="rows", required=false) Integer rows,
    		@RequestParam(value="sidx", required=false) String sidx,
    		@RequestParam(value="sord", required=false) String sord) {
		
		List<ReceiptItemCategoryDTO> receiptItemCategoryDTOList = null;
		Page<ReceiptItemCategoryTemplate> receiptItemCategoryPageList = null;
		
		JQGridResponse jqGridResponse = new JQGridResponse();
		Pageable pageRequest = new PageRequest(page-1, rows);
		
		receiptItemCategoryPageList = itemCategoryTemplateRepository.findAll(pageRequest);
		receiptItemCategoryDTOList = ReceiptItemCategoryMapper.map(receiptItemCategoryPageList);
		
		jqGridResponse.setPage(1);
		
		if(receiptItemCategoryPageList!= null) {
			
			jqGridResponse.setRecords(receiptItemCategoryPageList.getTotalElements());
			jqGridResponse.setTotal(receiptItemCategoryPageList.getTotalPages());
			jqGridResponse.setPage(receiptItemCategoryPageList.getNumber()+1);
		}
		jqGridResponse.setRows(receiptItemCategoryDTOList);
		return jqGridResponse;
	}
	
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('ADMIN')")
	public @ResponseBody MessageValue save(ReceiptItemCategoryTemplate receiptItemTemplateCategory) {
		logger.info("Receipt Item category passed: " + receiptItemTemplateCategory.getId() + "-value :" + receiptItemTemplateCategory.getValue());
		MessageValue message = new MessageValue();
		
		if(receiptItemTemplateCategory.getId()!=null && receiptItemTemplateCategory.getId().equals("_empty")) {
			receiptItemTemplateCategory.setId(null);
		}
		
		receiptItemCategoryTemplateService.save(receiptItemTemplateCategory);

		message.setMessage("File is uploaded...");
		//message.setDetailedMessage("http://farm4.staticflickr.com/3510/3208351775_e06db18ee4.jpg");

		return message;
	}
	
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	@PreAuthorize("hasAuthority('ADMIN')")
	public @ResponseBody MessageValue deleteReceiptItemCategory(@RequestParam("oper") String oper, @RequestParam("id") String receiptItemCategoryPID) {
		MessageValue message = new MessageValue();
		receiptItemCategoryTemplateService.delete(receiptItemCategoryPID);
		message.setMessage("Receipt Category deleted");
		return message;
	}
}
